# Maintainer: Ilya Rakhlin <i.rakhlin@gmail.com>

_realname=libressl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.0
pkgrel=1
pkgdesc="Free version of the TLS/crypto stack forked from OpenSSL (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.libressl.org'
msys2_references=(
  "cpe: cpe:/a:openbsd:libressl"
)
msys2_repository_url="https://github.com/libressl/portable"
license=('spdx:ISC AND OpenSSL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
optdepends=("${MINGW_PACKAGE_PREFIX}-ca-certificates")
options=('strip')
source=("https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/${_realname}-${pkgver}.tar.gz"{,.asc}
        "0001-relocation.patch"
        "0002-add-prefix.patch"
        "${msys2_repository_url}/commit/6551a0a5099a3ecdbfa248fe7cdd8a510b9e7ac0.diff"
        "pathtools."{c,h})
sha256sums=('4d841955f0acc3dfc71d0e3dd35f283af461222350e26843fea9731c0246a1e4'
            'SKIP'
            '55428e0371b2f02e71fb5942ba3bde1fd59bf8eb09294ac7f9917a6e3070fc2e'
            'e17ede3e4ccb4fd990cc5375834c91117e5cf1e483b63eb7ceea2110a30b6bfb'
            '03a47488606d0343abc11b5cd9e8ee498ae44df44f8cd5973c30aeba549d334c'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90')
validpgpkeys=('A1EB079B8D3EB92B4EBD3139663AF51BD5E4D8D5') # Brent Cook <bcook@openbsd.org>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"; do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}
# =========================================== #

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-relocation.patch \
    0002-add-prefix.patch \
    6551a0a5099a3ecdbfa248fe7cdd8a510b9e7ac0.diff
  echo crypto tls | xargs -n 1 cp "${srcdir}/pathtools."{c,h} -fHvt
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DOPENSSLDIR=" \
    cmake -G "Ninja" \
      -B "build-${MSYSTEM}" \
      -S "${_realname}-${pkgver}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_INCLUDEDIR=include/${_realname} \
      -DCMAKE_INSTALL_LIBDIR=lib/${_realname} \
      -DCMAKE_INSTALL_LIBEXECDIR=libexec/${_realname} \
      -DBUILD_SHARED_LIBS=ON \
      -DLIBRESSL_INSTALL_CMAKEDIR=lib/cmake/LibreSSL \
      -DOPENSSLDIR=${MINGW_PREFIX}/etc/${_realname} \
      "${extra_config[@]}"

  MSYS2_ARG_CONV_EXCL="-DOPENSSLBIN=;-DOPENSSLDIR=;-DTLS_DEFAULT_CA_FILE=" \
    cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"
  install -Dm644 "${_realname}-${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  # Resolve naming conflicts with OpenSSL
  find "${pkgdir}${MINGW_PREFIX}/share/man" -type f -print0 | xargs -0 rename -v '' "${_realname}-"
}
